<template>
  <div id="app">
    <transition name="slide">
      <Toast v-show="isShowToast" />
    </transition>
    <!-- <div class="iconfont icon-toast-shibai_huaban"></div> -->
    <Topbar :key="topbarKeyVal" />
    <Header />
    <router-view @updateKey="updateKey" />
    <Footer />
    <Login v-show="isShowLoginModal" />
    <Error />
  </div>
</template>
<script>
import Topbar from "@/components/Topbar.vue";
import Header from "@/components/Header.vue";
import Footer from "@/components/Footer.vue";
import Login from "@/components/Login.vue";
import Toast from "@/components/Toast.vue";

import { mapState } from "vuex";

// 安装axios包,引入axios
// import axios from "axios";

// 按需引入
import { JingpinAPI } from "@/request/api";
export default {
  data() {
    return {
      topbarKeyVal: 0,
    };
  },
  components: {
    Topbar,
    Header,
    Footer,
    Login,
    Toast,
<<<<<<< HEAD
    Error,
=======
>>>>>>> 42d7f4a3812cbaa13e12bafc11903e5f94f4bd8a
  },
  computed: {
    ...mapState({
      isShowLoginModal: (state) => state.showModal.isShowLoginModal,
      isShowToast: (state) => state.showToast.isShowToast,
    }),
  },
  async created() {
    /**
     * 弊端
     * 1. 每次发送请求都要输入很长的一段地址,一旦地址改动了,每一个地方都需要手动进行修改(不便于接口管理)
     * 2. 每次发起请求都需要携带token.(重复)
     * 3. 会有回调地狱问题
     *  例如: 请求a之后,再请求b
     */

    let res = await JingpinAPI().then((res) => {
      // console.log("响应数据:", res);
    });
    // let res1 = await JingpinAPI().then((res) => {
    //   console.log("响应数据1");
    // });
    // let res2 = await JingpinAPI().then((res) => {
    //   console.log("响应数据2");
    // });

    // JingpinAPI().then((res) => {
    //   console.log("响应数据:", res);
    // });

    // axios
    //   .get("/products/recommend")
    //   .then((res) => {
    //     console.log("请求成功:", res);
    //   })
    //   .catch((err) => {
    //     console.log("请求失败:", err);
    //   });
  },
  watch: {
    // 方式二:  组件的重载
    "$route.path": {
      handler(newVal, oldVal) {
        if (newVal != oldVal) {
          this.topbarKeyVal++;
        }
      },
    },
  },
  methods: {
    updateKey() {
      this.topbarKeyVal++;
    },
  },
};
</script>

<style lang="less">
// #app {
//   font-family: Avenir, Helvetica, Arial, sans-serif;
//   -webkit-font-smoothing: antialiased;
//   -moz-osx-font-smoothing: grayscale;
//   text-align: center;
//   color: #2c3e50;
// }

/* 入场的起始状态 = 离场的结束状态 */
.slide-enter,
.slide-leave-to {
  opacity: 0;
}

.slide-enter-active,
.slide-leave-active {
  transition: opacity 0.3s linear;
}

.slide-enter-to,
.slide-leave {
  opacity: 1;
}
</style>
